package com.hnxxxy.common.tools;

import com.github.pagehelper.PageInfo;
import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.sysmgr.pojo.CacheTableObj;
import com.hnxxxy.sysmgr.pojo.Config;
import com.hnxxxy.sysmgr.service.SysService;
import com.hnxxxy.usermgr.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * @CLASSDESCRIPER: 系统帮助类，主要写一些通用功能项
 * @AUTHOR:suhui
 * @PHONE:13319575368
 * @EMIAL:13319575368@163.COM
 * @DATE: 2020/6/11
 */
@Controller
@Slf4j
public class Help {
    private static   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
    @Autowired
    private SysService sysService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * @Description: 获取系统时间
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/6/11 13:50
     * @return: java.lang.String
     **/
    public static String getSystemDate(){
       return  sdf.format(new Date());
    }

    /**
     * @Description: 获取layui表单分页的方法
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/2510:13
     * @param tableObj:
     * @param list:
     * @return: com.hnxxxy.common.tools.TableObj
     **/
    public static TableObj getPageTableRtn(TableObj tableObj,List list){
        TableObj obj=tableObj;
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return obj.setTableObjInfo(obj,pageInfo);
    }

    /**
     * @Description: 文件上传公用方法
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/6/22 8:45
     * @return: null
     **/

        @ResponseBody
        @PostMapping("/upload/file/")
        public AjaxRtnEntity upload (@RequestParam("file") MultipartFile file) {
            AjaxRtnEntity ajaxRtnEntity;
            Config sysConfigInfo = sysService.getSysConfigInfo();
            String fileName = file.getOriginalFilename()+UUID.randomUUID();
            String filePath = sysConfigInfo.getUploadurl();
            fileName = filePath +File.separator+fileName;
            File dest = new File(fileName);
            if(!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
                ajaxRtnEntity =new AjaxRtnEntity(1,fileName,null);
            } catch (Exception e) {
                e.printStackTrace();
                ajaxRtnEntity =new AjaxRtnEntity(0,"文件上传失败",null);
            }
           return ajaxRtnEntity;
        }

    @RequestMapping("/download/file")
    public String downLoad(HttpServletResponse response,String filename,boolean flag) throws UnsupportedEncodingException {
        Config sysConfigInfo = sysService.getSysConfigInfo();
        String filePath = flag?sysConfigInfo.getUploadurl():sysConfigInfo.getDownurl();
        File file = new File(filePath +File.separator+ filename);
        if(file.exists()){ //判断文件父目录是否存在
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(filename,"UTF-8"));
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;
            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    @PostMapping("/clearCache")
    @ResponseBody
    public AjaxRtnEntity clearMybatisTableCache(@RequestBody List<CacheTableObj> list){
        for (CacheTableObj obj:list) {
            Set<String> keys = redisTemplate.keys(obj.getPattern());
            redisTemplate.delete(keys);
        }
        AjaxRtnEntity ajaxRtnEntity=new AjaxRtnEntity(1,"刷新缓存成功",null);
        return  ajaxRtnEntity;
    }



}
